// 防抖 执行最后一次 
export function debounce(fn, delay = 200) {
  let timer = null
  console.log(this)
  return function() {
    let arg = arguments
    timer && clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, arg)
    }, delay)
  }
}
// 节流固定时间内执行一次 
export function throttle(fn, time = 100) {
  var flag = false
  return function(e) { 
      if (flag) return
      flag = true
      console.log(this, arguments, 'gggggggg')
      fn.call(null, ...arguments)
      setTimeout(() => (flag = false), time)
  }
}
// 克隆数组对象
export function deepClone(source) {
  let result = Array.isArray(source) ? [] : {}
  Object.keys(source).forEach(key => {
    if (source[key] && typeof source[key] === 'object') {
      result[key] = deepClone(source[key])
    } else {
      result[key] = source[key]
    }
  })
  return result
}
// 判断两个对象或数组值是否一致
export function looseEqual (a, b) {
  if (a === b) return true
  const isObjectA = isObject(a)
  const isObjectB = isObject(b)
  if (isObjectA && isObjectB) {
    try {
      const isArrayA = Array.isArray(a)
      const isArrayB = Array.isArray(b)
      if (isArrayA && isArrayB) {
        return a.length === b.length && a.every((e, i) => {
          return looseEqual(e, b[i])
        })
      } else if (a instanceof Date && b instanceof Date) {
        return a.getTime() === b.getTime()
      } else if (!isArrayA && !isArrayB) {
        const keysA = Object.keys(a)
        const keysB = Object.keys(b)
        return keysA.length === keysB.length && keysA.every(key => {
          return looseEqual(a[key], b[key])
        })
      } else {
        /* istanbul ignore next */
        return false
      }
    } catch (e) {
      /* istanbul ignore next */
      return false
    }
  } else if (!isObjectA && !isObjectB) {
    return String(a) === String(b)
  } else {
    return false
  }
}
export function isObject (obj) {
  return obj !== null && typeof obj === 'object'
}
export function addOneDay(dateStr) {
  const date = new Date(dateStr);
  date.setDate(date.getDate() + 1);
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  return `${year}-${month}-${day}`;
}
export function subtractOneDay(dateString) {
  const date = new Date(dateString);
  date.setDate(date.getDate() - 1); // 减去一天
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
  const day = date.getDate().toString().padStart(2, '0');
  return `${year}-${month}-${day}`;
}
 
 
